package com.powermgr.boringtest.result;

import lombok.Data;

import java.util.Objects;

/**
 * Result
 *
 * @author qiuxiangdong
 * @date 2023/6/12
 */
@Data
public class Result<T> {

    private Integer code;
    private String message;
    private T data;

    public boolean isError() {
        return !Objects.equals(code, Code.SUCCESS.getCode());
    }
    public static Result<Void> success() {
        Result<Void> result = new Result<>();
        result.setCode(Code.SUCCESS.getCode());
        result.setMessage(Code.SUCCESS.getMessage());
        return result;
    }

    public static <V> Result<V> success(String message ,V data) {
        Result<V> result = new Result<>();
        result.code = Code.SUCCESS.getCode();
        result.message = message;
        result.data = data;
        return result;
    }

    public static Result<Void> failure() {
        Result<Void> result = new Result<>();
        result.setCode(Code.FAILURE.getCode());
        result.setMessage(Code.FAILURE.getMessage());
        return result;
    }

    public static <V> Result<V> failure(String message ,V data) {
        Result<V> result = new Result<>();
        result.code = Code.FAILURE.getCode();
        result.message = message;
        result.data = data;
        return result;
    }
}
